using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.Data.Entity.Sys;
using net_work.Data.VO.Sys;
using net_work.IRepository.Sys;
using net_work.IService.Sys;

namespace net_work.Service.Sys;

public class DepartmentService(
    IDepartmentRepository departmentRepository,
    ITeacherRepository teacherRepository,
    ICollegeRepository collegeRepository)
    : IDepartmentService
{
    public Task<PagedResult<DepartmentResult>> GetDepartmentsPagedAsync(PagedRequest request)
    {
        return departmentRepository.GetDepartmentPagedAsync(request);
    }

    public Task<Department?> SelectByIdAsync(long departmentId)
    {
        return departmentRepository.GetByIdAsync(departmentId);
    }

    public async Task<long> InsertDepartment(DepartmentAddRequest departmentAddRequest)
    {
        switch (departmentAddRequest.HeadTeacherId)
        {
            case null:
                break;
            case <= 0:
                return -1;
            default:
                var teacher = await teacherRepository.GetByIdAsync(departmentAddRequest.HeadTeacherId.Value);
                if (teacher == null)
                {
                    return -1;
                }

                break;
        }

        switch (departmentAddRequest.CollegeId)
        {
            case <= 0:
                return -1;
            default:
                var college = await collegeRepository.GetByIdAsync(departmentAddRequest.CollegeId);
                if (college == null)
                {
                    return -1;
                }

                break;
        }

        return await departmentRepository.InsertAsync(departmentAddRequest);
    }

    public async Task<int> UpdateDepartment(DepartmentUpdateRequest departmentUpdateRequest)
    {
        if (departmentUpdateRequest.Id <= 0)
        {
            return -1;
        }

        switch (departmentUpdateRequest.HeadTeacherId)
        {
            case null:
                break;
            case <= 0:
                return -1;
            default:
                var teacher = await teacherRepository.GetByIdAsync(departmentUpdateRequest.HeadTeacherId.Value);
                if (teacher == null)
                {
                    return -1;
                }

                break;
        }

        switch (departmentUpdateRequest.CollegeId)
        {
            case <= 0:
                return -1;
            default:
                var college = await collegeRepository.GetByIdAsync(departmentUpdateRequest.CollegeId);
                if (college == null)
                {
                    return -1;
                }

                break;
        }

        return await departmentRepository.UpdateAsync(departmentUpdateRequest) ? 1 : -1;
    }

    public async Task<int> DeleteDepartmentByIds(List<long> ids)
    {
        return await departmentRepository.DeleteByIdsAsync(ids) ? 1 : -1;
    }
}